const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
const operateMethod =require('../service/operatef.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        chairIsInUse: 1,
        isArriveScene: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.setData({
            taskId: options.taskId,
            operationType: options.type,
            hospitalId: options.hospitalId,
            operationlist: options.operationlist ? JSON.parse(options.operationlist) : [],
            operationTypeCode: options.operationTypeCode,
            activeKey: options.activeKey
        })
        that.getDetailById()
        that.getDeviceUseSituation()
    },
    //获取任务详情
    getDetailById() {
        let that = this
        lbbTaskService.getDetailById({
            taskId: that.data.taskId
        }, function (res) {
            that.setData({
                taskProjectText: res.data.taskProjectText,
                taskClassificationName: res.data.taskClassificationName,
                hospitalName: res.data.hospitalName,
                taskDesc: res.data.taskDesc,
                taskImages: res.data.taskImages ? res.data.taskImages.split(',') : [],
                isUrgent: res.data.isUrgent
            })
        })
    },
    //回显
    getDeviceUseSituation() {
        let that = this
        lbbTaskService.getDeviceUseSituation({
            taskId: that.data.taskId
        }, function (res) {
            if (res.data) {
                res.data.images = res.data.images ? res.data.images.split(',') : ''
                that.setData({
                    ...res.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //医院轮椅
    onChangeChairIsIn(e) {
        this.setData({
            chairIsInUse: e.detail
        })
    },
    //是否到现场
    onChangeArrive(e) {
        this.setData({
            isArriveScene: e.detail
        })
    },
    //备注
    changeRemark(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    //上传图片
    setImgArr(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1||item.indexOf('wxfile://tmp')!=-1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        wx.showLoading({
            title: '正在上传图片',
            mask: true
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            console.log('item================>', imgIdArray)
            wx.hideLoading()
            oldUpLoadImg.push(...imgIdArray)
            that.setData({
                images: oldUpLoadImg
            })
        });
    },
    //提交
    btnSubmit() {
        let that = this
        let obj = {
            chairIsInUse: that.data.chairIsInUse,
            isArriveScene: that.data.isArriveScene,
            remark: that.data.remark ? that.data.remark : '',
            images: that.data.images ? that.data.images.toString() : '',
            taskId: that.data.taskId
        }
        lbbTaskService.deviceUseSituation({
            situationInfo: JSON.stringify(obj)
        }, function (params) {
            if (that.data.activeKey == 3) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                operateMethod(that.data.operationlist, that.data.taskId, that.data.hospitalId, that.data.taskType, that.data.operationTypeCode)
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})